<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-claim">&nbsp;&nbsp;采购管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-circle-plus">&nbsp;&nbsp;订单发票添加</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div>
      <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
        <el-steps :active="1" finish-status="success" simple style="background-color: white">
          <el-step title="商品编码"></el-step>
          <el-step title="等待上步"></el-step>
          <el-step title="等待上步"></el-step>
          <el-step title="等待上步"></el-step>
        </el-steps>

        <el-divider></el-divider>

        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
          <el-form-item label="商品编码" prop="commodityCode">
            <el-input v-model="ruleForm.commodityCode" placeholder="请输入商品编码"></el-input>
          </el-form-item>
          <el-form-item>
<!--            <router-link :to="{path: '/sys-admin/purchase/oderAndInvoiceAdd2'}">-->
<!--              <el-button type="primary" style="color:white;margin-right: 20px">下一步</el-button>-->
<!--            </router-link>-->
            <el-button type="primary" @click="submitForm('ruleForm')" style="color:white;">下一步</el-button>
            <el-button type="warning" @click="resetForm('ruleForm')" style="color:white;">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data(){
    return{
      ruleForm:{
        commodityCode:'',
        commodityName:'',
      },
      rules:{
        commodityCode:[
          {required: true, message: '请输入商品编码', trigger: 'blur'},
          {pattern: /^\d+$/, message: '商品编码必须是数字', trigger: 'blur'}
        ],
      },
    }
  },
  methods: {
    loadLocalRuleForm() {
      let localRuleFormString = localStorage.getItem('localRuleForm');
      let localRuleForm = JSON.parse(localRuleFormString);
      if (localRuleForm.commodityCode) {
        this.ruleForm = localRuleForm;
      }
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 发请求，查询商品编码是否存在，如果均没有问题，则跳转到下一步
          let url = 'http://localhost:9080/purchaseOderandinvoices/commodityCode/' + this.ruleForm.commodityCode;
          console.log('url = ' + url);
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .get(url).then((response) => {
            let responseBody = response.data;

            if ( responseBody.state == 20000) {
              //let oderAndInvoice = responseBody.data;
              //this.ruleForm.commodityCode = oderAndInvoice.commodityCode;
              localStorage.setItem('localRuleForm', JSON.stringify(this.ruleForm));
              this.$router.push('oderAndInvoiceAdd2');
              }
            else {
              this.$alert(responseBody.message, '警告', {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },

    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.loadLocalRuleForm();
  }
}
</script>

<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}

</style>